package product;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;



public class MemberUpdate extends JInternalFrame  {

	Connection conn;
	ProductMain pm;
	PreparedStatement pstmt;
	ResultSet rs;
	ResultSetMetaData rsmd;
	MemberInput mi;
	MemberSelect ms;
	DefaultTableModel model = new DefaultTableModel();
	Vector<String> v;
	TableRowSorter<TableModel> tSoter;
	boolean tFlag = false;

	private JPanel contentPane;
	private JTextField codeIn;
	private JTextField phone;
	private JTextField mail;
	private JTextField irum;
	private JTextField address;
	private JTextField adPass;
	private JTextField date;
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel label_5;
	private JLabel label_6;
	private JLabel label_7;
	private JButton btnLogin;
	private JButton btnMod;

	private JTextField jumin;
	private JTextField payIn;
	private JLabel label_8;
	private JLabel label_9;

	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
	MemberData md;
	boolean mdf; //수정 확인

	String str1[]={"사원","대리","과장","부장"};//직급 콤보박스 배열
	String str2[]={"재경부","기획부","홍보부","개발부","마케팅부","총무부"};//부서 콤보박스 배열

	String position2="사원";//직급 콤보박스
	String team2="재경부";//부서 콤보박스



	Vector<String> colNam;	//여기서부터 전체조회
	String[] rows = {"사번","성명","직통번호","이메일","입사일","직책","부서"};
	Vector<String> RowData;

	private JLabel label_10;
	private JTextField adId;
	private JButton btnDel;
	private JButton btnSearch;
	private JButton btnLogout;
	private JButton btnClear;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;
	private JComboBox position;
	private JComboBox team;
	private JButton btnNewButton;
	private JButton btnall;

	public MemberUpdate(Connection conn, ProductMain p) {
		super("MemberUpdate", true, true, true, false);
		this.conn=conn;
		this.pm=p;

		v= new Vector<String>();

		v.add("No");
		v.add("Memberid");
		v.add("Pwd");
		v.add("Hiredate");
		v.add("Irum");
		v.add("Iden_number");
		v.add("team");
		v.add("Position");
		v.add("Phone");
		v.add("Email");
		v.add("Address");
		v.add("Pay");
		v.add("Working");

		model = new DefaultTableModel(v,0);


		setBounds(100, 100, 413, 331);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getCodeIn());
		contentPane.add(getPhone());
		contentPane.add(getMail());
		contentPane.add(getIrum());
		contentPane.add(getAddress());
		contentPane.add(getAdPass());
		contentPane.add(getDate());
		contentPane.add(getLblNewLabel());
		contentPane.add(getLabel());
		contentPane.add(getLabel_1());
		contentPane.add(getLabel_2());
		contentPane.add(getLabel_3());
		contentPane.add(getLabel_4());
		contentPane.add(getLabel_5());
		contentPane.add(getLabel_6());
		contentPane.add(getLabel_7());
		contentPane.add(getBtnLogin());
		contentPane.add(getBtnMod());

		contentPane.add(getJumin());
		contentPane.add(getPayIn());
		contentPane.add(getLabel_8());
		contentPane.add(getLabel_9());
		contentPane.add(getLabel_10());
		contentPane.add(getAdId());
		contentPane.add(getBtnDel());
		contentPane.add(getBtnSearch());
		contentPane.add(getBtnLogout());
		contentPane.add(getBtnClear());
		contentPane.add(getScrollPane());
		contentPane.add(getScrollPane_1());
		contentPane.add(getBtnall());



	}

	public void setI(MemberInput mi) {
		this.mi = mi;
	}
	public void setU(MemberSelect ms){
		this.ms = ms;
	}


	public void Modify(){

		try{
			if(!payIn.getText().equals("")){				

				if(!team.getSelectedItem().equals("")){				

					if(!position.getSelectedItem().equals("")){				


						String c= codeIn.getText();
						String i= irum.getText();
						String j= jumin.getText();
						String p= phone.getText();
						String e= mail.getText();
						String a= address.getText();
						String py= payIn.getText();

						String sql= "update memberdata set irum=?, iden_number=?, team=?, position=?, phone=?, email=?, address=?, pay=?  where memberid=?";



						pstmt= conn.prepareStatement(sql);
						pstmt.setString(1, i);
						pstmt.setString(2, j);
						pstmt.setString(3, team2);
						pstmt.setString(4, position2);
						pstmt.setString(5, p);
						pstmt.setString(6, e);
						pstmt.setString(7, a);
						pstmt.setString(8, py);
						pstmt.setString(9, c);
						pstmt.executeUpdate();
						pstmt.close();



						position2="사원";	
						team2="재경부";
						codeIn.setText("");
						irum.setText("");
						address.setText("");
						mail.setText("");
						phone.setText("");
						date.setText("");
						jumin.setText("");
						payIn.setText("");

						ms.columnadd();

						JOptionPane.showMessageDialog(null, "수정되었습니다.");

					}

				}

			}


		}catch(Exception e){
			e.printStackTrace();
//			JOptionPane.showMessageDialog(null, "급여는 숫자만 입력해주세요.");
		}
	}


	public void Delete(){
		try{

			int yn = JOptionPane.showConfirmDialog(null, "정말로 삭제하겠습니까?");

			if( yn == JOptionPane.YES_OPTION){

				String sql = "delete from memberdata where memberid=?";
				pstmt= conn.prepareStatement(sql);
				pstmt.setString(1, codeIn.getText());
				pstmt.executeUpdate();
				pstmt.close();

				JOptionPane.showMessageDialog(null, "정상적으로 삭제되었습니다");

				//position2="사원";		
				//team2="재경부";			
				irum.setText("");
				address.setText("");
				mail.setText("");
				adPass.setText("");
				phone.setText("");
				date.setText("");
				jumin.setText("");
				payIn.setText("");

				btnMod.setEnabled(false);
				btnDel.setEnabled(false);
				btnLogin.setEnabled(true);

				columnadd();

			}else{
				JOptionPane.showMessageDialog(null, "삭제를 취소하였습니다");		
			}

		}catch(Exception e){
			e.printStackTrace();
		}
	}


	public void adLogin(){
		//관리자 아이디로 접속이 되었을때,
		try{
			if(adId.getText().equals("AZ")){
				String sql = "select * from memberdata where memberid=?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, adId.getText());
				rs = pstmt.executeQuery();
				while(rs.next()){
					String key = rs.getString("pwd");
					if(key.equals(adPass.getText())){
						btnSearch.setEnabled(true);			

						adId.setText("****");
						adPass.setText("****");

						adId.setEnabled(false);
						adPass.setEnabled(false);
						codeIn.setEnabled(true);
						btnLogin.setEnabled(false);
						btnLogout.setEnabled(true);
						btnClear.setEnabled(true);
						btnMod.setEnabled(true);
						btnDel.setEnabled(true);
						payIn.setEnabled(true);
						btnall.setEnabled(true);

						tFlag = true;
					}
				}
				if(tFlag == false){
					JOptionPane.showMessageDialog(null, "패스워드가 다릅니다.");
				}

			}else{
				JOptionPane.showMessageDialog(null, "관리자 아이디가 아닙니다.");
			}

		}catch(Exception e){
			e.printStackTrace();
		}

	}


	public void serach_user(){
		try{
			String c = codeIn.getText();
			String sql = "select * from memberdata where memberid= ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, c);
			rs = pstmt.executeQuery();

			while (rs.next()) {
				
				String j = rs.getString("iden_number");
				String i = rs.getString("irum");
				String a = rs.getString("address");
				String p = rs.getString("phone");
				String e = rs.getString("email");
				String h = rs.getString("hiredate");
				String t = rs.getString("team");
				String po = rs.getString("position");
				String py = rs.getString("pay");

				position.setSelectedItem(po);
				team.setSelectedItem(t);

				irum.setText(i);
				jumin.setText(j);
				address.setText(a);
				phone.setText(p);
				mail.setText(e);
				date.setText(h);
				payIn.setText(py);

				btnMod.setEnabled(true);
				btnDel.setEnabled(true);

			}

		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public void columnadd() {

		try {    
			
			for(int i=model.getRowCount()-1;i>=0;i--){
				model.removeRow(i);
			}

			String sql = "select * from memberdata";
			pstmt = conn.prepareStatement(sql);

			rs = pstmt.executeQuery();// 리절트셋에 대한 부가정보를 갖는 객체얻어오기
			rsmd = rs.getMetaData();// 컬럼명 테이블에 셋팅

			int colCount = rsmd.getColumnCount();


			int k=0; 
			while (rs.next()) {
				v= new Vector<String>();
				k=k+1;
				v.add(""+k);

				for (int j = 1; j <= colCount; j++) {
					v.add(rs.getString(j));
				}
				model.addRow(v);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}



	public void clear(){
		codeIn.setText("");
		irum.setText("");
		address.setText("");
		mail.setText("");
		//team2="재경부";
		//position2="사원";
		adPass.setText("");
		phone.setText("");
		date.setText("");
		jumin.setText("");
		payIn.setText("");


		team.setEditable(false);
		position.setEditable(false);
		payIn.setEditable(false);
	}

	public void logout(){
		codeIn.setText("");
		irum.setText("");
		address.setText("");
		mail.setText("");
		//team2="재경부";
		//position2="사원";
		adPass.setText("");
		phone.setText("");
		date.setText("");
		jumin.setText("");
		payIn.setText("");

		adId.setText("");
		adPass.setText("");

		adId.setEnabled(true);
		adPass.setEnabled(true);


		codeIn.setEnabled(false);
		btnLogin.setEnabled(true);
		btnLogout.setEnabled(false);
		btnDel.setEnabled(false);
		btnMod.setEnabled(false);
		btnSearch.setEnabled(false);

		btnall.setEnabled(false);
		team.setEditable(false);
		position.setEditable(false);
		payIn.setEditable(false);
	}

//	public void search(){
//		try{ 
//
//			boolean save = true;
//
//			while(it.hasNext()){ // it에 키값이 존재하면 true 
//
//
//				if(save&&md.getCode().equals(codeIn.getText()) && md.getPwd().equals(adPass.getText())){
//
//					codeIn.setText(md.getCode());							
//					irum.setText(md.getIrum());								
//					mail.setText(md.getEmail());
//					position.setSelectedItem((md.getPosition()));
//					team.setSelectedItem(md.getTeam());
//					address.setText(md.getAddress());								
//
//					phone.setText(String.valueOf(md.getPhone()));
//					date.setText(sd.format(md.getjDate()));
//					jumin.setText(String.valueOf(md.getJumin()));
//					payIn.setText(String.valueOf(md.getdPay()));
//
//
//					//조회시 검색버튼 비활성
//
//					btnLogin.setEnabled(false);
//
//					//조회시 사번, 비번, 관리자 텍스트필드 수정 불가하게 비활성
//					codeIn.setEditable(false);
//					adPass.setEditable(false);
//					adId.setEditable(false);
//
//					//관리자로 접속시 
//				}else if(save&&md.getCode().equals(adId.getText()) && md.getPwd().equals(adPass.getText())){
//
//					codeIn.setText(md.getCode());							
//					irum.setText(md.getIrum());								
//					mail.setText(md.getEmail());
//					position.setSelectedItem((md.getPosition()));
//					team.setSelectedItem(md.getTeam());
//					address.setText(md.getAddress());								
//
//					phone.setText(String.valueOf(md.getPhone()));
//					date.setText(sd.format(md.getjDate()));
//					jumin.setText(String.valueOf(md.getJumin()));
//					payIn.setText(String.valueOf(md.getdPay()));
//
//
//
//					//조회시 수정 삭제 버튼 활성, 검색버튼 비활성
//					btnMod.setEnabled(true);
//					btnDel.setEnabled(true);
//					btnLogin.setEnabled(false);
//
//					//조회시 사번, 비번, 관리자 텍스트필드 수정 불가하게 비활성
//					codeIn.setEditable(false);
//					adPass.setEditable(false);
//					adId.setEditable(false);
//
//
//				}else if(!md.getCode().equals(adId.getText()) && !md.getPwd().equals(adPass.getText())){
//					JOptionPane.showMessageDialog(null, "아이디와 비밀번호가 일치하지 않습니다");								
//
//				}else if(!md.getCode().equals(codeIn.getText()) && !md.getPwd().equals(adPass.getText())){
//					JOptionPane.showMessageDialog(null, "아이디와 비밀번호가 일치하지 않습니다");								
//
//				}
//
//			}
//			// 사번 그리고 비번이 공백시 팜업창
//			if(save && (codeIn.getText().equals("") && 
//					adPass.getText().equals(""))){
//				JOptionPane.showMessageDialog(null, "공백을 채워주세요");
//
//				// 관리자 코드 그리고 비번이 공백시 팜업창
//			}else if(save && (adId.getText().equals("") && 
//					adPass.getText().equals(""))){
//				JOptionPane.showMessageDialog(null, "공백을 채워주세요");
//
//			}
//		}catch(NullPointerException ex){ 
//			JOptionPane.showConfirmDialog(null, "정보가 존재하지 않습니다");
//		}
//	}

	private JTextField getCodeIn() {
		if (codeIn == null) {
			codeIn = new JTextField();
			codeIn.setEnabled(false);
			codeIn.setBounds(74, 72, 116, 21);
			codeIn.setColumns(10);
		}
		return codeIn;
	}
	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setEditable(false);
			phone.setColumns(10);
			phone.setBounds(262, 103, 116, 21);
		}
		return phone;
	}
	private JTextField getMail() {
		if (mail == null) {
			mail = new JTextField();
			mail.setEditable(false);
			mail.setColumns(10);
			mail.setBounds(74, 165, 116, 21);
		}
		return mail;
	}
	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setEditable(false);
			irum.setColumns(10);
			irum.setBounds(74, 103, 116, 21);
		}
		return irum;
	}
	private JTextField getAddress() {
		if (address == null) {
			address = new JTextField();
			address.setEditable(false);
			address.setColumns(10);
			address.setBounds(262, 134, 116, 21);
		}
		return address;
	}
	private JTextField getAdPass() {
		if (adPass == null) {
			adPass = new JTextField();
			adPass.setHorizontalAlignment(SwingConstants.CENTER);
			adPass.setColumns(10);
			adPass.setBounds(74, 41, 116, 21);
		}
		return adPass;
	}
	private JTextField getDate() {
		if (date == null) {
			date = new JTextField();
			date.setEditable(false);
			date.setColumns(10);
			date.setBounds(262, 165, 116, 21);
		}
		return date;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("사원번호");
			lblNewLabel.setBounds(12, 75, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("연락처");
			label.setBounds(202, 106, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("이메일");
			label_1.setBounds(12, 168, 57, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("부서");
			label_2.setBounds(202, 199, 57, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("입사일");
			label_3.setBounds(200, 168, 57, 15);
		}
		return label_3;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("성명");
			label_4.setBounds(12, 106, 57, 15);
		}
		return label_4;
	}
	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("주소");
			label_5.setBounds(202, 137, 57, 15);
		}
		return label_5;
	}
	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("직책");
			label_6.setBounds(12, 199, 57, 15);
		}
		return label_6;
	}
	private JLabel getLabel_7() {
		if (label_7 == null) {
			label_7 = new JLabel("비밀번호");
			label_7.setBounds(12, 44, 57, 15);
		}
		return label_7;
	}
	private JButton getBtnLogin() {
		if (btnLogin == null) {
			btnLogin = new JButton("Login");
			btnLogin.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {					
					adLogin();
				}		
			});
			btnLogin.setBounds(262, 11, 116, 21);
		}
		return btnLogin;

	}

	private JButton getBtnMod() {
		if (btnMod == null) {
			btnMod = new JButton("수정");
			btnMod.setEnabled(false);
			btnMod.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {


					Modify();

				}

			});
			btnMod.setBounds(74, 257, 116, 21);

		}
		return btnMod;

	}
	private JTextField getJumin() {
		if (jumin == null) {
			jumin = new JTextField();
			jumin.setEditable(false);
			jumin.setColumns(10);
			jumin.setBounds(74, 134, 116, 21);
		}
		return jumin;
	}
	private JTextField getPayIn() {
		if (payIn == null) {
			payIn = new JTextField();
			payIn.setEnabled(false);
			payIn.setHorizontalAlignment(SwingConstants.RIGHT);
			payIn.setColumns(10);
			payIn.setBounds(74, 226, 116, 21);
		}
		return payIn;
	}
	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("주민번호");
			label_8.setBounds(12, 137, 57, 15);
		}
		return label_8;
	}
	private JLabel getLabel_9() {
		if (label_9 == null) {
			label_9 = new JLabel("일 급여");
			label_9.setBounds(12, 229, 57, 15);
		}
		return label_9;
	}
	private JLabel getLabel_10() {
		if (label_10 == null) {
			label_10 = new JLabel("관리자");
			label_10.setBounds(12, 14, 57, 15);
		}
		return label_10;
	}
	private JTextField getAdId() {
		if (adId == null) {
			adId = new JTextField();
			adId.setHorizontalAlignment(SwingConstants.CENTER);
			adId.setText("AZ");
			adId.setColumns(10);
			adId.setBounds(74, 10, 116, 21);
		}
		return adId;
	}
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton("삭제");
			btnDel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					Delete();

				}
			});
			btnDel.setEnabled(false);
			btnDel.setBounds(262, 257, 116, 21);
		}
		return btnDel;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("조회");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serach_user();
				}
			});
			btnSearch.setEnabled(false);
			btnSearch.setBounds(262, 72, 57, 21);
		}
		return btnSearch;
	}
	private JButton getBtnLogout() {
		if (btnLogout == null) {
			btnLogout = new JButton("Logout");
			btnLogout.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					logout();
				}
			});
			btnLogout.setEnabled(false);
			btnLogout.setBounds(262, 40, 116, 21);
		}
		return btnLogout;
	}
	private JButton getBtnClear() {
		if (btnClear == null) {
			btnClear = new JButton("Clear");
			btnClear.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					clear();
				}
			});
			btnClear.setEnabled(false);
			btnClear.setBounds(262, 225, 116, 21);
		}
		return btnClear;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(74, 195, 116, 19);
			scrollPane.setRowHeaderView(getPosition());
		}
		return scrollPane;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(262, 195, 116, 19);
			scrollPane_1.setRowHeaderView(getTeam());
		}
		return scrollPane_1;
	}
	private JComboBox getPosition() {
		if (position == null) {
			position = new JComboBox(str1);//직책 콤보박스
			position.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent ex) {
					if(ex.getStateChange() == ItemEvent.SELECTED){
						position2=(String)(ex.getItem());
						System.out.println(position2);
					}


				}
			});
			position.setPreferredSize(new Dimension(115, 15));
		}
		return position;
	}
	private JComboBox getTeam() {
		if (team == null) {
			team = new JComboBox(str2);//부서 콤보박스
			team.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent ev) {
					if(ev.getStateChange() == ItemEvent.SELECTED){
						team2=(String)(ev.getItem());
						System.out.println(team2);
					}


				}
			});
			team.setPreferredSize(new Dimension(115, 17));
		}
		return team;
	}
	public void setData(String c, String h, String i, String j, String t, String p, String pn, String em, String ad, String py){
		position.setSelectedItem(p);
		team.setSelectedItem(t);

		codeIn.setText(c);
		irum.setText(i);
		jumin.setText(j);
		address.setText(ad);
		phone.setText(pn);
		mail.setText(em);
		date.setText(h);
		payIn.setText(py);
		
//		btnMod.setEnabled(true);
//		btnDel.setEnabled(true);
	}
	private JButton getBtnall() {
		if (btnall == null) {
			btnall = new JButton("All");
			btnall.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					pm.openselect();
				}
			});
			btnall.setEnabled(false);
			btnall.setBounds(321, 71, 57, 23);
		}
		return btnall;
	}
}